<?php

use think\facade\Route;

Route::group(function () {
    Route::get('getDeviceData/:page/:pageSize/[:search]', 'DeviceData/getDeviceData');
})->middleware(function ($request, \Closure $next) {
    try {
        $authorization = request()->header('authorization');
        $timestamp = time() * 1000;
        $queryTimestamp = request()->header('timestamp');
        if (!$authorization || !$queryTimestamp) {
            throw new Exception('缺少授权参数');
        }

        if ($timestamp - $queryTimestamp > (1000 * 30)) {
            throw new Exception('请求超时');
        }

        $sign = hash_hmac('sha256', json_encode([
            'appid' => env('share_dashboard.app_id'),
            'timestamp' => (string)$queryTimestamp,
        ], 320), env('share_dashboard.key'));
        if (strcmp($authorization, $sign) === 0) {
            return $next($request);
        } else {
            throw new Exception('签名错误');
        }
    } catch (Exception $e) {
        return jsonReturn(1, $e->getMessage());
    }
});